<?php
class myTools
{
  public static function langFromCulture($culture)
  {
    list($lang, $tmp) = explode('_', $culture);
    return $lang;
  }
  
  public static function getCopyright()
  {
  	$years = date('Y') === sfConfig::get('app_site_start') ? sfConfig::get('app_site_start') : sfConfig::get('app_site_start').'&emdash;'.date('Y');
  	return '&copy;&nbsp;'.$years.sfConfig::get('app_site_name');
  }
  
  /**
   * Generates salt for password hashing
   *
   * @param string  $bizCardId      BizCard id of current user
   * @param string  $email          Email address of current user
   * @return string                 md5 hash
   */
  public static function makeSalt($string)
  {
    return md5(rand(100000, 999999).$string);
  }
  
  /**
   * Generates rendom string
   *
   * @return string       8 character long string of readable characters
   */
  public static function makeRandomString()
  {
    $string = '';	  
    for ($i=0; $i<8; $i++)
    {
      $string .= chr(mt_rand(35, 126));
    }
    return $string;
  }
  
  /**
   * Generates human pronouncable password
   *
   * @return string       8 character long string English letters and numbers
   */
  public static function makePassword()
  {    
    // Initialize valid char lists
    $consonant = 'bhjkmncdfgprstv';
    $vowel = 'aeiouy';
    $number = '1237840569'; 
    $length = 8;
    
    // Find the charset length
    $c_number = strlen($consonant) - 1;
    $v_number = strlen($vowel) - 1;
    $n_number = strlen($number) - 1;
    
    // Initialize the password and loop till we have all
    $password = false;
    
    while(strlen($password) < $length ) {
            // Pull out a random set of pronouncable chars
            if (mt_rand(0, 2) != 1){
                    $password .= $consonant[mt_rand(0, $c_number)] . $vowel[mt_rand(0, $v_number)] . $consonant[mt_rand(0, $c_number)];
            } else {
                    $password .= $number[mt_rand(0, $n_number)];
            }
    }
    
    $pass = substr($password, 0, $length);
        
    return $pass;   
  }
  
  public static function filterHtml($html)
  {
  	$config = HTMLPurifier_Config::createDefault();
  	$config->set('Cache', 'SerializerPath', sfConfig::get('sf_root_cache_dir').DIRECTORY_SEPARATOR.'purifier');
  	
  	$purifier = new HTMLPurifier($config);
  	return $purifier->purify($html);
  }
}
